package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.ACCID;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import static io.reactivex.internal.schedulers.SchedulerPoolFactory.start;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.Tezhi1RecyAdapter;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.MolelistBean;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils2;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 *
 */
public class Tezhi1Activity extends BaseActivity {

    private TextView btn_goup;
    private String tokens, mUserid;
    private RecyclerView rc_tezhi1;
    private Tezhi1RecyAdapter adapter;
    private int datingModeId;
    public static Tezhi1Activity instance;

    @Override
    protected void initListener() {
        btn_goup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Tezhi1Activity.this, Tezhi2Activity.class);
                MmkvUtil.INSTANCE.encode("datingModeId", datingModeId);
                startActivity(intent);

            }
        });
    }


    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_tezhi1);
        setStatusBarTranslucent();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            start();
            return false;

        } else {

            return super.onKeyDown(keyCode, event);

        }
    }

    @Override
    protected void initView() {
        instance = Tezhi1Activity.this;
        Intent intent;
        intent = new Intent();
        String token = intent.getStringExtra("token");
        mUserid = MmkvUtil.INSTANCE.decodeString(ACCID);
        LogUtils.i("tokenn", token);
        rc_tezhi1 = findViewById(R.id.rc_tezhi1);
        btn_goup = findViewById(R.id.btn_goup);
        getRequest();
        tokens = MmkvUtil.INSTANCE.decodeString(TOKEN);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getdatingModeList(tokens, EXTRA);
    }

    private void getdatingModeList(String token, String extra) {
        NetUtils2.getInstance().getApiService().getdatingModeList(token, extra)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("decryptStr", decryptStr);
                        Gson gson = new Gson();
                        MolelistBean molelistBean = gson.fromJson(decryptStr, MolelistBean.class);
                        //molelistbean是返回值
                        datingModeId = molelistBean.getData().getDatingModes().get(0).getId();
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(Tezhi1Activity.this);
                        rc_tezhi1.setLayoutManager(linearLayoutManager);
                        adapter = new Tezhi1RecyAdapter(molelistBean.getData().getDatingModes());
                        rc_tezhi1.setAdapter(adapter);
                        adapter.setOnAddClickListener(new Tezhi1RecyAdapter.OnAddClickListener() {
                            @Override
                            public void onItemClick(int id) {
                                datingModeId = id;
                            }
                        });
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
